﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Sale
{
    public partial class 辅料挂帐 : System.Web.UI.Page
    {
        string sql = @"SELECT t1.ID,t1.年度,t1.季度,t3.名称,t2.品名,t2.规格,t2.单位,t1.发票量,t1.单价,round(t1.发票量*t1.单价,2) as 金额,t1.开票日期,t1.挂帐日期,t1.合同编号 FROM 材料发票记录 t1
inner  join 材料 t2 on t2.ID=t1.材料ID
inner  join 材料供货单位 t3 on t3.ID=t1.供货ID";


        protected DateTime 起始日期, 截止日期;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.NianDu.SelectedValue = DateTime.Today.Year.ToString();
                if (DateTime.Today.Month == 1 || DateTime.Today.Month == 2 || DateTime.Today.Month == 3)
                {
                    this.JiDu.SelectedValue = "1";
                    GetData(this.NianDu.SelectedValue, "1", "3");
                }
                if (DateTime.Today.Month == 4 || DateTime.Today.Month == 5 || DateTime.Today.Month == 6)
                {
                    this.JiDu.SelectedValue = "2";
                    GetData(this.NianDu.SelectedValue, "4", "6");
                }
                if (DateTime.Today.Month == 7 || DateTime.Today.Month == 8 || DateTime.Today.Month == 9)
                {
                    this.JiDu.SelectedValue = "3";
                    GetData(this.NianDu.SelectedValue, "7", "9");
                }
                if (DateTime.Today.Month == 10 || DateTime.Today.Month == 11 || DateTime.Today.Month == 12)
                {
                    this.JiDu.SelectedValue = "4";
                    GetData(this.NianDu.SelectedValue, "10", "12");
                }
                this.Date1.Text = this.起始日期.ToString("yyyy年MM月dd日");
                this.Date2.Text = this.截止日期.ToString("yyyy年MM月dd日");
                this.SqlDataSource1.SelectCommand = sql + " order by t1.开票日期 desc";
                //this.Panel4.Visible = false;
                //this.SqlText.Text = this.SqlDataSource1.SelectCommand;
            }
            else
            {
                this.SqlDataSource1.SelectCommand = sql + " order by t1.开票日期 desc";
            }


        }
        public void GetData(string yy, string mm1, string mm2)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            string sql = @"
            declare @date1 smalldatetime
            declare @date2 smalldatetime
            select @date1=月初日期 from 供应处生产月设置 where 年度=@年度 and 月份=@月份1
            select @date2=月末日期 from 供应处生产月设置 where 年度=@年度 and 月份=@月份2
            select @date1,@date2";
            sql = sql.Replace("@年度", "'" + yy + "'");
            sql = sql.Replace("@月份1", "'" + mm1 + "'");
            sql = sql.Replace("@月份2", "'" + mm2 + "'");
            Cmd.CommandText = sql;
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                if (Reader.IsDBNull(0))
                {
                    起始日期 = DateTime.Parse("" + yy + "" + "-01-01");
                }
                else
                {
                    起始日期 = Reader.GetDateTime(0);
                }
                if (Reader.IsDBNull(1))
                {
                    截止日期 = DateTime.Parse("" + yy + "" + "-" + mm2 + "-28");
                }
                else
                {
                    截止日期 = Reader.GetDateTime(1);
                }
            }
            Reader.Close();
            Conn.Close();
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;


            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                this.SelectedID.Text = this.GridView1.SelectedDataKey[0].ToString();

                //    this.Button_View.Enabled = true;
                //    this.Button_View.OnClientClick = "parent.swin2_load('/Sale/采购原料合同查看.aspx?id=" + this.SelectedID.Text + "',760,600,'yes');return false;";
                //    if (this.SelectedID.Text != "")
                //    {
                //        this.Button_Edit.Enabled = true;

                //    }
                //    else
                //    {
                //        this.Button_Edit.Enabled = false;

                //    }
                this.Button_Edit.OnClientClick = "parent.swin2_load('/Sale/材料到货发票登记.aspx?id=" + this.SelectedID.Text + "',600,420);return false;";


            }
            catch
            {
                this.GridView1.SelectedIndex = -1;
            }
        }
        private string GetState()
        {
            string state = "";

            //if (this.BianHao.Text != "")
            //{
            //    state += " and t4.编号 like '%" + this.BianHao.Text.Replace("'", "''").Replace("[", "[[]").Replace("%", "[%]") + "%'";
            //}
            //if (this.NianDu.Text != "0")
            //{
            //    state += " and t5.年度=" + this.NianDu.Text;
            //}
            //if (this.JiDu.Text != "0")
            //{
            //    state += " and t5.季度=" + this.JiDu.Text;
            //}
            //if (this.LeiBie.Text != "0")
            //{
            //    state += " and t1.原料ID=" + this.LeiBie.Text;
            //}
            //if (this.GongHuo.Text != "0")
            //{
            //    state += " and t1.供货ID=" + this.GongHuo.Text;
            //}

            //if (state.StartsWith(" and"))
            //{
            //    state = state.Substring(4);
            //}
            //if (state != "")
            //{
            //    state = " where " + state;
            //}
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            //if (this.GongHuo.SelectedValue != "0" && this.LeiBie.SelectedValue != "0")
            //{
            //this.Panel4.Visible = true;
            //                SqlConnection Conn = new SqlConnection();
            //                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            //                Conn.Open();
            //                SqlCommand Cmd = new SqlCommand();
            //                Cmd.Connection = Conn;
            //                string sql0 = @"
            //                declare @暂估入库量 float
            //                declare @合同量  int
            //                select @暂估入库量=isnull(sum(磅重),0) from 原料到货记录 where 原料ID=@原料ID and 供货ID=@供货ID and 日期 between '@起始日期' and '@截止日期' 
            //                select @合同量 =isnull(sum(数量),0) from 采购原料合同 where 原料ID=@原料ID and 供货ID=@供货ID and 年度=@年度 and 季度=@季度
            //                select @暂估入库量,@合同量";
            //                sql0 = sql0.Replace("@年度", this.NianDu.Text);
            //                sql0 = sql0.Replace("@季度", this.JiDu.Text);
            //                sql0 = sql0.Replace("@原料ID", this.LeiBie.Text);
            //                sql0 = sql0.Replace("@供货ID", this.GongHuo.Text);
            //                sql0 = sql0.Replace("@起始日期", DateTime.Parse(this.Date1.Text).ToShortDateString());
            //                sql0 = sql0.Replace("@截止日期", DateTime.Parse(this.Date2.Text).ToShortDateString());
            //                Cmd.CommandText = sql0;
            //                SqlDataReader Reader = Cmd.ExecuteReader();

            //                if (Reader.Read())
            //                {
            //                    this.Label_Result.Text = "";
            //                    this.Label_Result.Text = "<span style='display:inline-block;width:300px;'>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  暂估入库量：" + (double)Reader[0]
            //                            + " 吨(支)</span><span style='display:inline-block;width:200px;'>合同量：" + (int)Reader[1]
            //                            + " 吨(支)</span><br>";
            //                }
            //            }
            //            else
            //            {
            //                this.Panel4.Visible = false;
            //            }
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = sql + " order by t1.开票日期 desc";
        }

        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }


        protected void JiDu_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NianDu.SelectedValue = DateTime.Today.Year.ToString();
            if (this.JiDu.SelectedValue == "1")
            {
                GetData(this.NianDu.SelectedValue, "1", "3");
            }
            if (this.JiDu.SelectedValue == "2")
            {
                this.JiDu.SelectedValue = "2";
                GetData(this.NianDu.SelectedValue, "4", "6");
            }
            if (this.JiDu.SelectedValue == "3")
            {
                this.JiDu.SelectedValue = "3";
                GetData(this.NianDu.SelectedValue, "7", "9");
            }
            if (this.JiDu.SelectedValue == "4")
            {
                this.JiDu.SelectedValue = "4";
                GetData(this.NianDu.SelectedValue, "10", "12");
            }
            this.Date1.Text = this.起始日期.ToString("yyyy年MM月dd日");
            this.Date2.Text = this.截止日期.ToString("yyyy年MM月dd日");
        }

        protected void NianDu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.JiDu.SelectedValue == "1")
            {
                GetData(this.NianDu.SelectedValue, "1", "3");
            }
            if (this.JiDu.SelectedValue == "2")
            {
                this.JiDu.SelectedValue = "2";
                GetData(this.NianDu.SelectedValue, "4", "6");
            }
            if (this.JiDu.SelectedValue == "3")
            {
                this.JiDu.SelectedValue = "3";
                GetData(this.NianDu.SelectedValue, "7", "9");
            }
            if (this.JiDu.SelectedValue == "4")
            {
                this.JiDu.SelectedValue = "4";
                GetData(this.NianDu.SelectedValue, "10", "12");
            }
            this.Date1.Text = this.起始日期.ToString("yyyy年MM月dd日");
            this.Date2.Text = this.截止日期.ToString("yyyy年MM月dd日");
        }



        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.Button_Delete.CommandName = this.GridView1.SelectedDataKey[0].ToString();
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.Button_Delete.CommandName;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·删除失败";
            }
        }
    }
}
